package cn.itcast.reentrantlock;

import cn.itcast.test1.util.Sleeper;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
@Slf4j(topic = "c.Demo03")
public class Demo03 {

    private static final ReentrantLock LOCK = new ReentrantLock();
    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            try {
                if(!LOCK.tryLock(2, TimeUnit.SECONDS)){
                   log.debug("尝试获取锁失败");
                   return;
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            try {
                log.debug("t1获取到锁");
            }finally {
                LOCK.unlock();
            }
        });

        LOCK.lock();
        log.debug("获取锁");
        t1.start();
        Sleeper.sleep(1);
        LOCK.unlock();
        log.debug("释放锁");
    }
}
